package com.linln.modules.tribe.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.tribe.domain.Tribe;
import com.linln.modules.tribe.repository.TribeRepository;
import com.linln.modules.tribe.service.TribeService;

/**
 * @author 左书亮
 * @date 2019/10/10
 */
@Service
public class TribeServiceImpl implements TribeService {

    @Autowired
    private TribeRepository tribeRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Tribe getById(Long id) {
        return tribeRepository.findById(id).orElse(null);
    }

    
    public List<Tribe> getList(List<Long> ids){
    	return tribeRepository.findAllById(ids);
    }

    public  Boolean updateIsCollectAddById(Long id) {
    	return tribeRepository.updateIsCollectAddById(id) > 0;
    }
    
    public  Boolean updateIsReplyAddById(Long id) {
    	return tribeRepository.updateIsReplyAddById(id) > 0;
    }

    public Boolean updateIsZanAddById(Long id){
        return tribeRepository.updateIsZanAddById(id) > 0;

    }

	
	public  Boolean updateIsCollectSubById(Long id) {
		return tribeRepository.updateIsCollectSubById(id) > 0;
	 }
	
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Tribe> getPageList(Example<Tribe> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        
        return tribeRepository.findAll(example, page);
    }
    @Override
    public Page<Tribe> getPageList(Example<Tribe> example,Integer pageIndex,Integer pageSize) {
        // 创建分页对象
    	 Sort sort = new Sort(Sort.Direction.DESC, "id");
         if(pageIndex==null) {
         	pageIndex=1;
         }    	PageRequest page = PageSort.pageRequest(pageIndex,pageSize,sort);

        return tribeRepository.findAll(example, page);
    }
    /**
     * 保存数据
     * @param tribe 实体对象
     */
    @Override
    public Tribe save(Tribe tribe) {
        return tribeRepository.save(tribe);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return tribeRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}